package com.fc.example.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;

import com.anythink.core.api.ATAdConst;
import com.anythink.core.api.ATBiddingListener;
import com.anythink.core.api.ATBiddingResult;
import com.anythink.splashad.unitgroup.api.CustomSplashAdapter;
import com.fc.example.CustomApplication;
import com.yfanads.android.core.splash.YFAdSplashAds;
import com.yfanads.android.core.splash.YFSplashListener;
import com.yfanads.android.model.YFAdError;
import com.yfanads.android.utils.ScreenUtil;
import com.yfanads.android.utils.YFLog;
import com.yfanads.android.utils.YFUtil;

import java.util.Map;
import java.util.Objects;
import java.util.UUID;

/**
 * 开屏适配器
 **/
public class YFSplashAdapter extends CustomSplashAdapter {
    private static final String TAG = "YFSplashAdapter";
    String mUnitid;
    String appid;
    private YFAdSplashAds fcAdSplash;
    private int width = 0; // 单位px
    private int height = 0; // 单位px

    @Override
    public void loadCustomNetworkAd(Context context, Map<String, Object> serverExtras, Map<String, Object> localExtras) {
        if (serverExtras.containsKey("appid") && serverExtras.containsKey("unitid")) {
            appid = (String) serverExtras.get("appid");
            mUnitid = (String) serverExtras.get("unitid");
            YFUtil.switchMainThread(() -> startLoadAds(context, localExtras, null));
        } else {
            if (mLoadListener != null) {
                mLoadListener.onAdLoadError("", "appid or unitid is empty!");
            }
        }
    }

    @Override
    public boolean startBiddingRequest(Context context, Map<String, Object> serverExtras, Map<String, Object> localExtras, ATBiddingListener biddingListener) {
        if (serverExtras.containsKey("appid") && serverExtras.containsKey("unitid")) {
            appid = (String) serverExtras.get("appid");
            mUnitid = (String) serverExtras.get("unitid");
            YFUtil.switchMainThread(() -> startLoadAds(context, localExtras, biddingListener));
        } else {
            if (mLoadListener != null) {
                mLoadListener.onAdLoadError("", "appid or unitid is empty!");
            }
        }
        YFLog.debug(TAG + " startBiddingRequest appid_" + appid + ",mUnitid_" + mUnitid);
        return true;
    }

    @Override
    public void show(Activity activity, ViewGroup viewGroup) {
        if (fcAdSplash != null) {
            fcAdSplash.showAds(activity, viewGroup);
        }
    }

    private void startLoadAds(Context context, Map<String, Object> localExtras, ATBiddingListener biddingListener) {
        try {
            if (localExtras.containsKey(ATAdConst.KEY.AD_WIDTH)) {
                width = Integer.parseInt(Objects.requireNonNull(localExtras.get(ATAdConst.KEY.AD_WIDTH)).toString());
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }

        try {
            if (localExtras.containsKey(ATAdConst.KEY.AD_HEIGHT)) {
                height = Integer.parseInt(Objects.requireNonNull(localExtras.get(ATAdConst.KEY.AD_HEIGHT)).toString());
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        YFLog.info(TAG + " startLoadAds width_" + width + ",height_" + height);
        width = ScreenUtil.px2dip(context, width);
        height = ScreenUtil.px2dip(context, height);
        YFInit.init(CustomApplication.getInstance(), appid);
        fcAdSplash = new YFAdSplashAds(context, new YFSplashListener() {
            @Override
            public void onAdSuccess() {
                if (biddingListener != null) {
                    long ecpm = fcAdSplash.getEcpm();
                    YFLog.debug(TAG + " SplashAd ecpm : " + ecpm);
                    if (ecpm < 0) {
                        ecpm = 0;
                    }
                    //获取货币单位
                    ATAdConst.CURRENCY currency = ATAdConst.CURRENCY.RMB_CENT;
                    //用于标识此次竞价的唯一ID，可使用UUID或者时间戳
                    String token = UUID.randomUUID().toString();
                    biddingListener.onC2SBiddingResultWithCache(ATBiddingResult.success(ecpm, token, null, currency), null);
                } else {
                    if (mLoadListener != null) {
                        mLoadListener.onAdCacheLoaded();
                    }
                }
            }

            @Override
            public void onAdExposure() {
                YFLog.debug(TAG + " onAdExposure");
                if (mImpressionListener != null) {
                    mImpressionListener.onSplashAdShow();
                }
            }

            @Override
            public void onAdClicked() {
                YFLog.debug(TAG + " onAdClicked");
                if (mImpressionListener != null) {
                    mImpressionListener.onSplashAdClicked();
                }
            }

            @Override
            public void onAdClosed() {
                YFLog.debug(TAG + " onAdClosed");
                if (mImpressionListener != null) {
                    mImpressionListener.onSplashAdDismiss();
                }
            }

            @Override
            public void onAdFailed(YFAdError yfAdError) {
                YFLog.debug(TAG + " onAdFailed " + yfAdError.toString());
                if (biddingListener != null) {
                    biddingListener.onC2SBiddingResultWithCache(ATBiddingResult.fail(yfAdError.msg), null);
                } else if (mLoadListener != null) {
                    mLoadListener.onAdLoadError(yfAdError.code, yfAdError.msg);
                }
            }

            @Override
            public void onAdRenderFailed() {
                YFLog.debug(TAG + " onAdRenderFailed");
                if (mImpressionListener != null) {
                    mImpressionListener.onSplashAdShowFail(null);
                }
            }

        });
        if (width != 0 && height != 0) {
            fcAdSplash.setWidth(width);
            fcAdSplash.setHeight(height);
        }
        fcAdSplash.loadOnly(mUnitid);
    }


    @Override
    public void destory() {
        YFLog.debug(TAG + " destroy");
        if (fcAdSplash != null) {
            fcAdSplash.destroy();
        }
    }

    @Override
    public String getNetworkPlacementId() {
        return mUnitid;
    }

    @Override
    public String getNetworkSDKVersion() {
        return YFInit.sdkVersion;
    }

    @Override
    public String getNetworkName() {
        return YFInit.networkName;
    }

    @Override
    public boolean isAdReady() {
        return fcAdSplash != null && fcAdSplash.isReady();
    }
}
